import logging
import requests, re, json, time
from lxml import etree
from templates.logger import Loggers

class times:
    def __init__(self):
        global logger
        logger = Loggers().make_logger('getTimeForecast')
        logger.info('Clmib seven days start loading .........')

    def seven_days(self, city_code):
        respones = requests.get('http://www.weather.com.cn/weather/%s.shtml' % city_code)
        respones.encoding = 'utf-8'
        global contents, html
        contents = respones.text
        html = etree.HTML(contents)
        # city_name
        city_name = ''
        province_name = re.findall('<a href="http://\w+.weather.com.cn" target="_blank">(.*?)</a>', contents)[0]  # 省
        try:
            city_name = re.findall('target="_blank">(.*?)</a><span>></span>', contents)[0]  # 市
        except:
            logger.warning(city_name)

        try:
            district_name = re.findall('<span>></span>\n\s*<span>(.*?)</span>', contents)[0]  # 区(有点地方是3个)
        except:
            district_name = re.findall('<span>></span>\s*<span>(.*?)</span>', contents)[0]  # 区
        adress = province_name + '-' + city_name + '-' + district_name
        logger.info(adress)
        # 最近7天的天气概况
        server_day = re.findall('var hour3data=({.*})', contents)[0]  # 查找最近7天的天气概况
        results = json.loads(server_day)  # 序列化
        result = results['7d']
        logger.info(result)
        # print( "=" * 25 + "living" + "=" * 25)
        # 生活指数
        dict_live = {}
        new_dictLive = {}
        for i in range(1, 8):
            for j in range(1, 7):
                if j == 2:  # 减肥指数
                    tittle = html.xpath('//*[@id="livezs"]/div[%d]/ul/li[%d]/a/em/text()' % (i, j))  # 指数
                    star = 0
                    for a in range(1, 6):  # 获取星级
                        intro = html.xpath('//*[@id="livezs"]/div[%d]/ul/li[%d]/a/span/em[%d]/@class' % (i, j, a))  # 简介
                        if intro:
                            star += 1
                    content = html.xpath('//*[@id="livezs"]/div[%d]/ul/li[%d]/a/p/text()' % (i, j))  # 建议
                    dict_live["%s" % tittle[0]] = '%s:星' % star, content[0]
                    continue
                if j == 4:  # 穿衣指数
                    cy = html.xpath('//*[@id="chuanyi"]/a/em/text()')  # 穿衣指数
                    cy_intro = html.xpath('//*[@id="chuanyi"]/a/span/text()')  # 简介
                    cy_content = html.xpath('//*[@id="chuanyi"]/a/p/text()')  # 建议
                    dict_live["%s" % cy[0]] = cy_intro[0], cy_content[0]
                    continue
                date = html.xpath('//*[@id="7d"]/ul/li[%d]/h1/text()' % i)[0]  # 获取当天的时间
                tittle = html.xpath('//*[@id="livezs"]/div[%d]/ul/li[%d]/em/text()' % (i, j))  # 指数
                intro = html.xpath('//*[@id="livezs"]/div[%d]/ul/li[%d]/span/text()' % (i, j))  # 简介
                content = html.xpath('//*[@id="livezs"]/div[%d]/ul/li[%d]/p/text()' % (i, j))  # 建议
                dict_live["%s" % tittle[0]] = intro[0], content[0]
                new_dictLive[date] = str(dict_live)
        logger.info(new_dictLive)
        return adress, result, new_dictLive

    def start(self, city_code=101270101):
        adress, result, new_dictLive = self.seven_days(city_code)
        return city_code, adress, result, new_dictLive

    def __del__(self):
        logger.info('Clmib successful')


if __name__ == '__main__':
    s = time.clock()
    adress, result, new_dictLive = times().seven_days(101270101)
    # print(adress, result, new_dictLive)
    # print(len(result))
    e = time.clock()
    print(e - s)
